package com.hongye.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hongye.common.entity.BrokerEntity;
import com.hongye.manager.mapper.BrokerMapper;
import com.hongye.manager.service.BrokerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrokerServiceImpl extends ServiceImpl<BrokerMapper, BrokerEntity> implements BrokerService {

    @Autowired
    private BrokerMapper brokerMapper;

    @Override
    public boolean isExist(BrokerEntity brokerEntity) {

        QueryWrapper<BrokerEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("broker_name",brokerEntity.getBrokerName());

        if (brokerEntity.getId() != null){
            wrapper.ne("id",brokerEntity.getId());
        }

        return count(wrapper) > 0;
    }

    @Override
    public void update(BrokerEntity brokerEntity) {
        int i = baseMapper.updateById(brokerEntity);
    }

    @Override
    public List<BrokerEntity> selectUserPage(Page<BrokerEntity> page,String name) {

        QueryWrapper<BrokerEntity> qw = new QueryWrapper<>();
        qw.like("broker_name",name);

        IPage<BrokerEntity> page1 = baseMapper.selectPage(page, qw);
        List<BrokerEntity> records = page1.getRecords();
        return records;
    }

}
